﻿
namespace SvGame.Controller;

public partial class GameHandler
{
    /// <summary>
    /// 技能列表
    /// </summary>
    /// <param name="pack"></param>
    public int C2GSkills(MyPackage pack)
    {
        pack.GetMsg(out C2GSkills req);
        var rsp = new G2CSkills();

        rsp.Tag = req.Tag;
        if (!pack.PackGetPlayer(out var player) || player is null)
        {
            return 0;
        }

        foreach (var kvp in player.Skills)
        {
            var node = new InfoSkill()
            {
                Id = kvp.Key,
                Level = kvp.Value,
            };
            rsp.Skills[node.Id] = node;
        }
        var usages = player.UsageGetAll();
        foreach (var item in usages.Values)
        {
            rsp.Usages[item.Usage] = item;
        }

        return pack.Send(rsp);
    }
    /// <summary>
    /// 技能升级
    /// </summary>
    /// <param name="pack"></param>
    public int C2GSkillLevelup(MyPackage pack)
    {
        pack.GetMsg(out C2GSkillLevelup req);
        var rsp = new G2CSkillLevelup();

        if (!pack.PackGetPlayer(out var player) || player is null)
        {
            return 0;
        }

        player.Skills.TryGetValue(req.SkillId, out var lv);
        if (lv <= 0)
        {
            return player.MessageError("您还没有学会这项技能");
        }
        var potneed = GameLogic.GetPotLevelup(lv);
        if (player.Pot < potneed)
        {
            return pack.Send(rsp, EProtocolResult.潜能不足);
        }

        // todo C2GSkillLevelup 获取消耗和判断后面要重构
        player.Pot -= potneed;
        var lvnew = lv + 1;
        player.Skills[req.SkillId] = lvnew;
        rsp.Skill = new InfoSkill()
        {
            Id = req.SkillId,
            Level = lvnew,
        };

        player.NtfPlayerInfo();
        return pack.Send(rsp);
    }
    /// <summary>
    /// 技能应用
    /// </summary>
    /// <param name="pack"></param>
    public int C2GSkillUse(MyPackage pack)
    {
        pack.GetMsg(out C2GSkillUse req);
        var rsp = new G2CSkillUse();

        if (!pack.PackGetPlayer(out var player) || player is null)
        {
            return 0;
        }

        if (!ConfListUsage.Contains(req.Usage))
        {
            return player.MessageError("技能激发错误");
        }
        if (!req.Usage.TryParseEnum<EUsageType>(out var usage))
        {
            return player.MessageError("技能激发错误");
        }
        if (req.Skill.IsNullOrEmpty() || req.Skill == "无")
        {
            player.Usages[req.Usage] = "无";
        }
        else
        {
            if (!player.Skills.ContainsKey(req.Skill))
            {
                return player.MessageError("您还没有学会这项技能");
            }
            if (!ConfSkill.TryGetValue(req.Skill, out var confskill))
            {
                return player.MessageError("没有这项技能");
            }
            if (!confskill.Usage.Contains(req.Usage))
            {
                return player.MessageError("这项技能不能用做这种用途");
            }
            player.Usages[req.Usage] = req.Skill;
        }
        player.ActionSet();
        player.HpSet();

        player.UsageGet(usage, out var skillId, out var usageLevel);
        rsp.Usage = new InfoUsage()
        {
            Usage = req.Usage,
            Skill = skillId,
            UsageLevel = usageLevel,
        };

        player.NtfPlayerInfo();
        return pack.Send(rsp);
    }
}
